package com.workcell.workdata.im.component.validator;

import com.workcell.workdata.im.annotation.validator.RobotMessageFormat;
import com.workcell.workdata.im.constant.MessageFormat;
import com.workcell.workdata.im.model.message.ChatLink;
import com.workcell.workdata.im.model.request.RobotMessageRequest;
import com.workcell.workdata.im.util.JSON;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class RobotMessageValidator implements ConstraintValidator<RobotMessageFormat, RobotMessageRequest> {

    @Override
    public boolean isValid(RobotMessageRequest request, ConstraintValidatorContext context) {

        if (request.getFormat() == MessageFormat.TEXT){
            return true;
        }

        if (request.getFormat() == MessageFormat.LINK){
            ChatLink link = JSON.parseNullable(request.getContent(), ChatLink.class);
            return link != null && StringUtils.isNotBlank(link.title) && StringUtils.isNotBlank(link.url);
        }

        return false;
    }

}
